package com.huawang.mdesign.model.service.util;

import com.huawang.core.definition.mof.instances.MetaClassInstance;
import com.huawang.core.semantics.entity.MofPropertyInfo;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.logging.log4j.util.Strings;

import java.util.*;

/**
 * Mof属性工具类
 */
public class MofPropertyUtil {

    private MofPropertyUtil() {}
    /**
     * 通过mof属性获取属性的字符串值
     * @param mofPropertyInfo mof属性信息
     * @return 属性字符值
     */
    public static String getStringValueFromMofPropertyInfo(MofPropertyInfo mofPropertyInfo) {
        if (mofPropertyInfo != null) {
            Object values = mofPropertyInfo.getValues();
            if (values instanceof List) {
                if (!((List) values).isEmpty()) {
                    return ((List) values).get(0).toString();
                }
            } else if (values != null) {
                return values.toString();
            } else {
                return Strings.EMPTY;
            }
        }
        return Strings.EMPTY;
    }

    /**
     * 通过mof属性获取属性的字符串值
     * @param mofPropertyInfo mof属性信息
     * @return 属性字符值
     */
    public static Double getDoubleValueFromMofPropertyInfo(MofPropertyInfo mofPropertyInfo) {
        if (mofPropertyInfo != null) {
            Object values = mofPropertyInfo.getValues();
            if (values instanceof List) {
                if (!((List) values).isEmpty()) {
                    return Double.valueOf(((List) values).get(0).toString());
                }
            } else if (values != null) {
                return Double.valueOf(values.toString());
            } else {
                return null;
            }
        }
        return null;
    }

    /**
     * 通过mof属性获取属性的Double列表
     * @param mofPropertyInfo mof属性信息
     * @return 属性字符值
     */
    public static List<Double> getDoubleListValueFromMofPropertyInfo(MofPropertyInfo mofPropertyInfo) {
        if (mofPropertyInfo != null) {
            Object values = mofPropertyInfo.getValues();
            if (values instanceof List) {
                return (List)values;
            }
        }
        return new ArrayList<>();
    }

    /**
     * 获得实例属性的值
     */
    public static MetaClassInstance getInstanceValueFromMofPropertyInfo(MofPropertyInfo mofPropertyInfo) {
        return Optional.ofNullable(mofPropertyInfo)
                .map(MofPropertyUtil::getListInstanceByProperty)
                .filter(CollectionUtils::isNotEmpty)
                .map(e -> e.get(0))
                .orElse(null);
    }

    /**
     * 根据属性获得实例模型列表
     */

    public static List<MetaClassInstance> getListInstanceByProperty(MofPropertyInfo mofPropertyInfo) {
        return Optional.ofNullable(mofPropertyInfo)
                .map(MofPropertyInfo::getValues)
                .filter(List.class::isInstance)
                .map(MofPropertyUtil::objToList)
                .filter(CollectionUtils::isNotEmpty)
                .orElse(Collections.emptyList());
    }

    @SuppressWarnings("unchecked")
    public static List<MetaClassInstance> objToList(Object value) {
        return Objects.nonNull(value) ? (List<MetaClassInstance>) value : Collections.emptyList();
    }
}
